module scene {

    import RankPage = page.RankPage;
    import WorldRankPage = page.WorldRankPage;

    export class GameRankScene extends eui.Component {

        public tabLimitTimeRank: eui.Group;
        public tabGroupRank: eui.Group;
        public tabFriendRank: eui.Group;
        public tabWorldRank: eui.Group;

        public btnFightImg: eui.Image;
        public btnBackImg: eui.Image;

        public rankPageGroup: eui.Group;

        private friendRank: RankPage;

        private groupRank: RankPage;

        private limitRank: RankPage;

        private worldRank: WorldRankPage;

        private currentRank: any;

        public btnPrevPageImg: eui.Label;
        public btnNextPageImg: eui.Label;

        private btnBack: ui.PushButton;

        private btnFight: ui.PushButton;

        private rootGroup: eui.Group;

        public bgImg: eui.Image;

        private mask1: eui.Image
        private mask2: eui.Image;

        private tabBtns: Array<ui.TabbarButton> = new Array<ui.TabbarButton>();

        public constructor() {
            super();
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "resource/assets/GameRes/game/skins/GameRankSceneSkin.exml";
        }


        private tabGroupButton: eui.Group;

        public onComplete(eve: eui.UIEvent): void {

            this.bgImg.width = manager.DisplayManager.STAGE_WIDTH;
            this.bgImg.height = manager.DisplayManager.STAGE_HEIGHT;

            this.tabFriendRank.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onFriendRank, this);

            this.tabWorldRank.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onWorldRank, this);

            this.btnPrevPageImg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPrevPage, this);

            this.btnNextPageImg.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onNextPage, this);

            this.btnBack = new ui.PushButton(this.btnBackImg);
            this.rootGroup.addChild(this.btnBack);
            this.btnBack.addEventListener(ui.PushButton.CLICK, this.onBack, this);

        }

        private onBack(): void {
            let openDataContext = wx.getOpenDataContext();
            openDataContext.postMessage({
                command: 0,
            });
            
            manager.DisplayManager.remove(this);
        }

        public update(): void {
                // this.selected(this.tabFriendRank);
                this.showFriendRank();
        }

        public pageBtnsGroup: eui.Group;

        private onPrevPage(eve: egret.Event): void {
            egret.log("this.currentRank:", this.currentRank);
            if (this.currentRank != null && this.currentRank == this.worldRank) {
                egret.log("this.currentRank2:", this.currentRank);
                if (this.worldRank != null) {
                    this.worldRank.bigRank.onPrevPage();
                }
                return;
            }
            RankPage.sendCommand(RankPage.PREV_PAGE);
        }

        private onNextPage(eve: egret.Event): void {
            egret.log("this.currentRank3:", this.currentRank);
            if (this.currentRank != null && this.currentRank == this.worldRank) {
                egret.log("this.currentRank4:", this.currentRank);
                if (this.worldRank != null) {
                    this.worldRank.bigRank.onNextPage();
                }
                return;
            }
            RankPage.sendCommand(RankPage.NEXT_PAGE);
        }

        private selected(group: eui.Group): void {

        }

        private onFriendRank(eve: egret.TouchEvent): void {
            this.showFriendRank();
        }

        private onWorldRank(eve: egret.TouchEvent): void {
            // this.selected(eve.currentTarget);
            this.showWorldRank();
        }

        private showWorldRank(): void {
            this.mask1.visible = true
            this.mask2.visible = false
            this.hideAllRanks();
            if (this.worldRank == null) {
                this.worldRank = new page.WorldRankPage();
            }
            this.currentRank = this.worldRank;
            this.worldRank.reload();
            this.rankPageGroup.addChild(this.worldRank);
            this.worldRank.visible = true;
        }

        private showFriendRank(): void {
            this.mask1.visible = false
            this.mask2.visible = true
            this.hideAllRanks();
            if (this.friendRank == null) {
                this.friendRank = new page.RankPage();
            }
            this.currentRank = this.friendRank;
            this.rankPageGroup.addChild(this.friendRank);
            this.friendRank.show(page.RankPage.SHOW, null);

            
        }

        private hideAllRanks(): void {
            manager.DisplayManager.removeAllChildren(this.rankPageGroup);
            if (this.limitRank) {
                this.limitRank.hide();
            }
            if (this.friendRank) {
                this.friendRank.hide();
            }
            if (this.groupRank) {
                this.groupRank.hide();
            }
            if (this.worldRank) {
                this.worldRank.hide();
            }
        }
    }

}